$(document).ready(function () {
    var ctx = $("#ctx").val();//获取服务器地址
    /***************************    活动列表页    ***************************/
    $("#btn-create").click(function () {
    });

    //状态全部、开启、停用
    if ($("#status").val() == 0 || $("#status").val() == "") {
        $("#selectBar_status a:first").addClass("selectBar_change");
        $("#status").val(0);
    }
    $("#selectBar_status a").each(function () {
        var value = $(this).attr("value");
        if ($("#status").val() == value) {
            $(this).addClass("selectBar_change").siblings('.selectBar_change').removeClass('selectBar_change');
        }
    });

    $("#selectBar_status a").click(function () {
        $("#status").val($(this).attr("value"));
        refreshData();
    });

    //批量开启,批量暂停
    $("#all-btn-enable,#all-btn-disable,#all-btn-delete").click(function () {
        if ($(this).attr("id") == "all-btn-enable") {
            url = ctx + "/advertiser/main/ad/target/ad-group-alloperator/enable";
        } else if ($(this).attr("id") == "all-btn-disable") {
            url = ctx + "/advertiser/main/ad/target/ad-group-alloperator/disable";
        } else if ($(this).attr("id") == "all-btn-delete") {
            url = ctx + "/advertiser/main/ad/target/ad-group-alloperator/delete";
        }
        batchOpear($(this), url);
    });

    //删除
    $(".btn-delete").click(function () {
        url = ctx + "/advertiser/main/ad/target/ad-group-alloperator/delete";
        singleOper($(this), url);
    });
    /***************************    创建，编辑投放    ***************************/

    //请选择点击类型
    if ($("#main_perClickul").html()) {
        if ($("#perClickType").val() == 0 || $("#perClickType").val() == "") {
            $("#perClickul_span").text("请选择点击类型");
        }
        $('ul#perClick_son_ul li').each(function () {
            if ($(this).attr("value") == $("#perClickType").val()) {
                $("#perClickul_span").text($(this).text());
            }
        });
        $("#perClick_son_ul").hide();
        $('ul#perClick_son_ul li').live("click", function () {
            $(this).parents('li').find('span').text($(this).html());
            $("#perClickType").val($(this).attr("value"));
            $("#perClickTypeName").val($(this).text());
            $('#perClick_son_ul').hide(); //ul隐藏
        });
        $("#perClickul_span").on("click", function () {
            $('#perClick_son_ul').show(); //ul显示
        });
        $("#perClickul_box").mouseleave(function () {
            $('#perClick_son_ul').hide(); //ul隐藏
        });
    }

    //请选择曝光类型
    if ($("#main_perImpressionul").html()) {
        if ($("#perImpressionType").val() == 0 || $("#perImpressionType").val() == "") {
            $("#perImpressionul_span").text("请选择曝光类型");
        }
        $('ul#perImpression_son_ul li').each(function () {
            if ($(this).attr("value") == $("#perImpressionType").val()) {
                $("#perImpressionul_span").text($(this).text());
            }
        });
        $("#perImpression_son_ul").hide();
        $('ul#perImpression_son_ul li').live("click", function () {
            $(this).parents('li').find('span').text($(this).html());
            $("#perImpressionType").val($(this).attr("value"));
            $("#perImpressionTypeName").val($(this).html());
            $('#perImpression_son_ul').hide(); //ul隐藏
        });
        $("#perImpressionul_span").on("click", function () {
            $('#perImpression_son_ul').show(); //ul显示
        });
        $("#perImpressionul_box").mouseleave(function () {
            $('#perImpression_son_ul').hide(); //ul隐藏
        });
    }

    //地域定向
    if ($("#areasDiv").html()) {
        var areasgeoPopup = $("#areas_popup")[0];
        var GEO_list;
        $('#btn-areas').click(function () {
            if (GEO_list) areasgeoPopup.show();
            else {
                $.ajax({
                    url: ctx + '/advertiser/main/ad/target/ad-group-alloperator/getLocations',
                    dataType: 'json',
                    success: function (GEO_LIST) {
                        Area.init(GEO_LIST, selectedIds);
                        areasgeoPopup.show();
                        GEO_list = GEO_LIST;
                    },
                    error: function () {
                        alert('数据拉取失败，请检查您的网络连接是否可用');
                    }
                });
            }

            if (!areasgeoPopup) {
                areasgeoPopup = new areasPopup({
                    width: '746px',
                    height: '435px',
                    title: '地域定向设定'
                });
                var defaultValue = "不限";
                var input = $('#areas').trigger('change');
                var selectedIds = input.val() ? input.val().split(',') : null;
                $('#areasconfirm').click(function () {
                    var pop = areasgeoPopup;
                    areasgeoPopup.hide();
                    input.val(Area.save());
                    var id = 'areas', selected = Area.save();
                    $('#areas').val(selected);
                    if (selected && $('input[type=checkbox]:not(:checked)').length) {
                        //有选择 且 非全选的
                        $('#count-areas').text(selected.split(',').length);
                        $('#geo-all-areas').hide();
                        $('#geo-selected-areas').show();
                    } else {
                        $('#geo-all-areas').show();
                        $('#geo-selected-areas').hide();
                    }
                });
            } else {
                areasgeoPopup.show();
            }
        });

        $('#count-areas').click(function () {
            $('#btn-areas').trigger('click');
        });

        if ($('#areas').val()) {
            $('#geo-selected-areas').show();
            $('#geo-all-areas').hide();
            $('#count-areas')[0].innerHTML = $('#areas').val().split(',').length;
        } else {
            $('#geo-selected-areas').hide();
            $('#geo-all-areas').show();
        }
    }
    //时间定向
    $('#setting_list').unbind("mousedown").bind("mousedown", function () {
        var date = $("#dateRangePicker").val().split('to');
        var s_date = date[0];
        var e_date = date[1];
        var len_box = $(".time_box").find(".time_item").length;
        if (s_date != "" && e_date != "") {
            $("#quantum_list1").find("p").removeClass("pitchon");
            $('#setting_list').daterangepicker1({
                showDropdowns: true,
                linkedCalendars: true,
                autoUpdateInput: true,
                "startDate": s_date,
                "endDate": e_date,
                "minDate": s_date,
                "maxDate": e_date,
                locale: {
                    direction: $('#rtl').is(':checked') ? 'rtl' : 'ltr',
                    format: 'YYYY-MM-DD',
                    separator: ' to ',
                    applyLabel: '确定',
                    cancelLabel: '取消',
                    fromLabel: '起始时间',
                    toLabel: '结束时间',
                    daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                    monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                        '七月', '八月', '九月', '十月', '十一月', '十二月'],
                    firstDay: 1
                }
            });
            $(".daterangepicker1").eq($(".daterangepicker1").length - 1).addClass("add");
        } else {
            bootbox.alert("请选择活动周期，在进行操作！");
        }
    });

    //初始校验表单
    if ($("#validateForm").html()) {
        $('#dateRangePicker').daterangepicker({
            showDropdowns: true,
            linkedCalendars: true,
            autoUpdateInput: true,
            locale: {
                direction: $('#rtl').is(':checked') ? 'rtl' : 'ltr',
                format: 'YYYY-MM-DD',
                separator: ' to ',
                applyLabel: '确定',
                cancelLabel: '取消',
                fromLabel: '起始时间',
                toLabel: '结束时间',
                daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                    '七月', '八月', '九月', '十月', '十一月', '十二月'],
                firstDay: 1
            }
        }, function (start, end, label) {//格式化日期显示框  
            $('#dateRangePicker').val(start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
            $(".setting .time_box").empty();
        });
        if ($(".add_activeTime").html()) {
            $('#dateRangePicker').val("");
        }
        $("#validateForm").autovalidate();
        $(".input-text").focus(function () {
            $(this).parent("div.input-group").find("span.help-inline").text("").removeClass("help-inline").addClass("spanVaildate");
        });
    }
    var time_item_index = 0;
    $(document).on('click', '.daterangepicker1.add .applyBtn', function () {
        if ($(".edit_timeDiv").html() && $(".time_box .time_item").length > 0) {
            time_item_index = $(".time_box .time_item").length + 10;
        }
        var date = $("#dateRangePicker").val().split('to');
        var s_date = date[0];
        var e_date = date[1];
        var checked_date = [];
        var len = $(".setting .time_item").length;
        for (var i = 0; i < len; i++) {
            var json_check = {
                "startDate": $(".time_item").eq(i).find(".start_date").val(),
                "endDate": $(".time_item").eq(i).find(".end_date").val()
            };
            checked_date.push(json_check);
        }
        var startDate = $(this).parents(".daterangepicker1").find("input[name='daterangepicker_start']").val();
        var endDate = $(this).parents(".daterangepicker1").find("input[name='daterangepicker_end']").val();
        var flag = true;
        for (var j = 0; j < checked_date.length; j++) {
            if (startDate < checked_date[j].startDate && endDate < checked_date[j].startDate) {
                console.log(startDate + ":" + endDate);
                console.log(flag + ":1");
            } else if (startDate > checked_date[j].endDate && endDate > checked_date[j].endDate) {
                console.log(flag + ":2");
            } else {
                flag = false;
            }
        }
        if (flag) {
            var timeData = "";
            var obj = $(this).parents(".daterangepicker1").find("#quantum_list1");
            if (obj.find(".pitchon").length) {
                timeData = obj.find(".pitchon").map(function (i, span) {
                    return $(span).attr("title");
                }).toArray().join(',');
            } else {
                timeData = -1;
            }
            var html = '<div class="time_item" index="' + time_item_index + '"><p class="tr_data"><span class="inp"><input type="" class="start_date edit_time" readonly="readonly" value="' + startDate + '"></span><span class="inp"><input type="" class="end_date edit_time" readonly="readonly" value = "' + endDate + '"></span></p><button class="btn_del">删除</button><input class="json_date" type="hidden" value = "' + timeData + '"></div>';
            $(".setting .time_box").append(html);
        } else {
            bootbox.alert("请选择正确时间！");
        }
        time_item_index++;
    });
    var index_item = 0;
    $(document).on('click', '.daterangepicker1.edit .applyBtn', function () {
        var date = $("#dateRangePicker").val().split('to');
        var s_date = date[0];
        var e_date = date[1];
        var checked_date = [];
        var len = $(".setting .time_item").length;
        for (var i = 0; i < len; i++) {
            var json_check = {
                "startDate": $(".time_item").eq(i).find(".start_date").val(),
                "endDate": $(".time_item").eq(i).find(".end_date").val()
            };
            checked_date.push(json_check);
        }
        var startDate = $(this).parents(".daterangepicker1").find("input[name='daterangepicker_start']").val();
        var endDate = $(this).parents(".daterangepicker1").find("input[name='daterangepicker_end']").val();
        var timeData = "";
        var obj = $(this).parents(".daterangepicker1").find(".quantum_list");
        if (obj.find(".pitchon")) {
            timeData = obj.find(".pitchon").map(function (i, span) {
                return $(span).attr("title");
            }).toArray().join(',');
        } else {
            timeData = -1;
        }
        $(".time_box .time_item").each(function () {
            if (index_item == $(this).attr("index")) {
                $(this).find(".start_date").val(startDate);
                $(this).find(".end_date").val(endDate);
                $(this).find(".json_date").val(timeData);
            }
        });
    });
    $(document).on('mousedown', '.setting .tr_data', function () {
        var date = $("#dateRangePicker").val().split('to');
        var s_date = date[0];
        var e_date = date[1];
        var obj_tr = $(this);
        var th_date_s = $(this).find(".start_date").val();
        var th_date_e = $(this).find(".end_date").val();
        obj_tr.daterangepicker1({
            showDropdowns: true,
            linkedCalendars: true,
            autoUpdateInput: true,
            "startDate": th_date_s,
            "endDate": th_date_e,
            "minDate": s_date,
            "maxDate": e_date,
            locale: {
                direction: $('#rtl').is(':checked') ? 'rtl' : 'ltr',
                format: 'YYYY-MM-DD',
                separator: ' to ',
                applyLabel: '确定',
                cancelLabel: '取消',
                fromLabel: '起始时间',
                toLabel: '结束时间',
                daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                    '七月', '八月', '九月', '十月', '十一月', '十二月'],
                firstDay: 1
            }
        });
        var obj = $(".daterangepicker1").eq($(".daterangepicker1").length - 1);
        $(".daterangepicker1").eq($(".daterangepicker1").length - 1).find(".quantum_list p").removeClass("pitchon");
        var chose_time = obj_tr.parent().find(".json_date").val();
        if (chose_time != -1 && chose_time != "") {
            var chose_arr = chose_time.split(",");
            for (var n = 0; n < chose_arr.length; n++) {
                $(".daterangepicker1").eq($(".daterangepicker1").length - 1).find(".quantum_list p").eq(chose_arr[n]).addClass("pitchon");
            }
        }
        index_item = $(this).parent(".time_item").attr("index");
        if (!obj.hasClass("edit")) {
            $(".daterangepicker1").eq($(".daterangepicker1").length - 1).addClass("edit");
            $(".daterangepicker1").eq($(".daterangepicker1").length - 1).attr("index", index_item);
        }
    });
    $(document).on('click', '.daterangepicker1 .cancelBtn', function () {
        $(this).parents(".daterangepicker1").css("display", "none");
    });
    $(document).on('click', '.time_box .time_item .btn_del', function () {
        $(this).parents(".time_item").remove();
    });

    $("#create-btn-save,#edit-btn-save").click(function () {
        $("div.input-group").find("span.help-inline").text("").removeClass("help-inline").addClass("spanVaildate");
        if (!$("#validateForm").validate()) {
            return false;
        }
        //时段定向
        var len = $(".setting .time_item").length;
        var jsondata = "[";
        for (var i = 0; i < len; i++) {
            if (i == 0) {
                jsondata += '{"startDate":\"' + $(".time_item").eq(i).find(".start_date").val() + '\","endDate":\"' + $(".time_item").eq(i).find(".end_date").val() + '\","dehours":\"' + $(".time_item").eq(i).find(".json_date").val() + '\"}';
            } else {
                jsondata += ',{"startDate":\"' + $(".time_item").eq(i).find(".start_date").val() + '\","endDate":\"' + $(".time_item").eq(i).find(".end_date").val() + '\","dehours":\"' + $(".time_item").eq(i).find(".json_date").val() + '\"}';
            }
        }
        jsondata += "]";
        $("#sdayParting").val(jsondata);
        //提示操作系统必填
        var oslist = $("#osDiv input[type='checkbox']:checked");
        var osId = "";
        if (oslist.length > 0) {
            oslist.each(function () {
                osId += $(this).val() + ",";
            });
            if (osId != "") {
                osId = osId.substring(0, osId.length - 1);
            }
        }
        $("#os").val(osId);
        //性别
        var genderlist = $("#genderDiv input[type='checkbox']:checked");
        var genderId = "";
        if (genderlist.length > 0) {
            genderlist.each(function () {
                genderId += $(this).val() + ",";
            });
            if (genderId != "") {
                genderId = genderId.substring(0, genderId.length - 1);
            }
        }
        $("#gender").val(genderId);
        //年龄和性别
        var agelist = $("#ageDiv input[type='checkbox']:checked");
        var ageId = "";
        if (agelist.length > 0) {
            agelist.each(function () {
                ageId += $(this).val() + ",";
            });
            if (ageId != "") {
                ageId = ageId.substring(0, ageId.length - 1);
            }
        }
        $("#age").val(ageId);
        //保存整体页面
        $("#validateForm").submit();
        return false;
    });

    $("#create-btn-cancel,#edit-btn-cancel").click(function () {
        window.location.href = ctx + "/advertiser/main/ad/target/ad-group-alloperator/list?&orderId=" + $("#orderId").val() + "&advertiserId=" + $("#advertiserId").val() + "&page=" + $("#page").val();
    });

    //操作系统
    if ($(".edit_osDiv").html()) {
        setting_date();//获取排期时间
        var osId = $("#os").val();
        var authArr = osId.split(",");
        var checkboxs = $("#osDiv input[type='checkbox']");
        checkboxs.each(function (i) {
            for (var i = 0; i < authArr.length; i++) {
                if ($(this).val() == authArr[i]) {
                    $(this).attr("checked", "true");
                }
            }
        });
    }

    //性别
    if ($(".edit_genderDiv").html()) {
        var genderId = $("#gender").val();
        var authArr = genderId.split(",");
        var checkboxs = $("#genderDiv input[type='checkbox']");
        checkboxs.each(function (i) {
            for (var i = 0; i < authArr.length; i++) {
                if ($(this).val() == authArr[i]) {
                    $(this).attr("checked", "true");
                }
            }
        });
    }
    //年龄
    if ($(".edit_ageDiv").html()) {
        var ageId = $("#age").val();
        var authArr = ageId.split(",");
        var checkboxs = $("#ageDiv input[type='checkbox']");
        checkboxs.each(function (i) {
            for (var i = 0; i < authArr.length; i++) {
                if ($(this).val() == authArr[i]) {
                    $(this).attr("checked", "true");
                }
            }
        });
    }
    /******  选择媒体关联  *****/
    $("#btn-selectOperator").click(function () {
        var mode = $('input[name=mode]:checked').val();
        var dailyMaxImpression = $('#dailyMaxImpression').val();
        var dailyMaxClick = $('#dailyMaxClick').val();
        var adType = $('input:radio[name="adtype"]:checked').val();
        //获取创意个数
        var bannerNum = $('#bannerNum').val();
        if (mode == 1 && dailyMaxImpression == "") {
            bootbox.alert("请先填写每日曝光总上限");
            return false;
        } else if (mode == 2 && dailyMaxClick == "") {
            bootbox.alert("请先填写每日点击总上限");
            return false;
        } else if (adType == "") {
            bootbox.alert("请先填写广告类型");
            return false;
        } else if (bannerNum == 0 && adType != 6) {
            bootbox.alert("请先进行创意上传");
            return false;
        }
        layer.open({
            type: 2,
            title: '选择媒体',
            shadeClose: false,
            shade: 0.6,
            fix: false,
            area: ['1060px', '475px'],
            offset: ['10px'],
            content: [ctx + '/advertiser/main/ad/target/ad-group-alloperator/chooseOperator?deviceType=' + $('#deviceType').val() + '&adGroupId=' + $("#adGroupId").val() + '&mode=' + mode + '&price=' + $('#price').val() + '&dailyMaxClick=' + dailyMaxClick + '&dailyMaxImpression=' + dailyMaxImpression + '&isEdit=' + $('#isEdit').val() + '&adType=' + adType, 'no']
        });
    });

    $("#btn-selectAdx").click(function () {
        var mode = $('input[name=mode]:checked').val();
        layer.open({
            type: 2,
            title: '关联ADX',
            shadeClose: false,
            shade: 0.6,
            fix: false,
            area: ['1060px', '436px'],
            offset: ['10px'],
            content: [ctx + '/advertiser/main/ad/target/ad-group-alloperator/relateAdx?adGroupId=' + $("#adGroupId").val() + '&mode=' + mode + '&price=' + $('#price').val(), 'no']
        });
    });

    //定量设置
    $("#btn-quantity").click(function () {
        layer.open({
            type: 2,
            title: '定量设置',
            shadeClose: false,
            shade: 0.6,
            fix: false,
            area: ['900px', '436px'],
            content: [ctx + '/advertiser/main/ad/target/ad-group-alloperator/quantity?adGroupId=' + $("#adGroupId").val(), 'no']
        });
    });

    /***************************    启用 暂停按钮    ***************************/
    $('.switch input').change(function () {
        if ($(this).is(':checked')) {
            //$(this).parent().addClass('switPlay'); // 双重保险，但IE低版本不兼容css3的波浪号时可以通过switPlay控制
            url = ctx + "/advertiser/main/ad/target/ad-group-alloperator/enable";
            $(this).parent().siblings('.switchText').html('开启');
        } else {
            url = ctx + "/advertiser/main/ad/target/ad-group-alloperator/disable";
            $(this).parent().siblings('.switchText').html('停用');
        }
        singleOper($(this), url);
    });

    $('#cpm-radio').click(function () {
        $('#cpm-text').show();
        $('#cpc-text').hide();
    });
    $('#cpc-radio').click(function () {
        $('#cpm-text').hide();
        $('#cpc-text').show();
    });

    $('.create-btn-save1,#circle1').click(function () {
        $('#circle1').addClass('active');
        $('#circle2').removeClass('active');
        $('#circle3').removeClass('active');
        $('#step1').show();
        $('#step2').hide();
        $('#step3').hide();
    });
    $('.create-btn-save2,#circle2').click(function () {
        var adGroupName = $('#adGroupName').val();
        var dateRangePicker = $("#dateRangePicker").val();
        var totalBudget = $('#totalBudget').val();
        var speedMode = $('#speedMode').val();
        var buyType = $('#buyType').val();
        var mode = $('input:radio[name="mode"]:checked').val();
        var price = $('#price').val();
        var deviceType = $('#deviceType').val();
        if (adGroupName == "" || adGroupName == null) {
            bootbox.alert("请填写策略名称");
            return false;
        }
        if (dateRangePicker == "" || dateRangePicker == null) {
            bootbox.alert("请选择活动周期");
            return false;
        }
        if (totalBudget == "" || totalBudget == null) {
            bootbox.alert("请填写策略总预算");
            return false;
        }
        if (price == "" || price == null) {
            bootbox.alert("请填写策略单价");
            return false;
        }
        if (deviceType == 1 || deviceType == 2) {
            $('#mobileType').hide();
            $('#mobileType input').removeAttr('checked');
            $('#jsType').show();
            if (!$('#jsType input[checked="checked"]').length) {
                $('#jsType input[value="1"]').attr('checked', 'checked');
            }
            $('#btn-selectAdx').hide();
        } else {
            $('#jsType').hide();
            $('#jsType input').removeAttr('checked');
            $('#mobileType').show();
            if (!$('#mobileType input[checked="checked"]').length) {
                $('#mobileType input[value="7"]').attr('checked', 'checked');
            }
            $('#btn-selectAdx').show();
        }
        $('#circle1').removeClass('active');
        $('#circle2').addClass('active');
        $('#circle3').removeClass('active');
        $('#step1').hide();
        $('#step2').show();
        $('#step3').hide();
    });
    $('.create-btn-save3,#circle3').click(function () {
        $('#circle1').removeClass('active');
        $('#circle2').removeClass('active');
        $('#circle3').addClass('active');
        $('#step1').hide();
        $('#step2').hide();
        $('#step3').show();
    });
});

function exportExcel_list() {
    var searchValue = $("#searchValue").val();
    if (searchValue == "请输入关键字") {
        searchValue = "";
    }
    var param = "?orderId=" + $("#orderId").val() + "&advertiserId=" + $("#advertiserId").val() + "&status=" + $("#status").val() + "&searchValue=" + searchValue;
    $("#exportExcel").attr("href", $("#ctx").val() + "/advertiser/main/ad/target/ad-group-alloperator/exportExcel" + param);
    return false;
}

function validateMoney() {
    var reg = /^\d{1,8}\.?\d{0,2}$/;
    var $this = $("#totalBudget");
    if ($this.val().match(reg) != null) {
        if (($this.val().indexOf('.') == -1 && $this.val().length > 8) || $this.val() <= 0) {
            $this.css("box-shadow", "0 0 6px #ff0000").attr('title', '总预算金额整数位最多8位');
            $this.parent("div").find("span.spanVaildate").html("总预算金额整数位最多8位");
            return false;
        } else if ($this.val() > parseInt($("#budget").val())) {
            $this.css("box-shadow", "0 0 6px #ff0000").attr('title', '总预算不能大于活动总预算');
            $this.parent("div").find("span.spanVaildate").html("总预算不能大于活动总预算");
            return false;
        } else {
            $this.css("box-shadow", "none").attr('title', '');
            $this.parent("div").find("span.spanVaildate").html("");
        }
    } else {
        $this.css("box-shadow", "0 0 6px #ff0000").attr('title', '总预算金额整数位最多8位且最多有2位小数');
        $this.parent("div").find("span.spanVaildate").html("总预算金额整数位最多8位且最多有2位小数");
        return false;
    }
}

function setting_date() {
    var str_date = $("#sdayParting").val();
    if (str_date != "") {
        var js_date = $.parseJSON(str_date);
        var html = "";
        for (var i = 0; i < js_date.length; i++) {
            html += '<div class="time_item" index="' + i + '"><p class="tr_data"><span class="inp"><input type="text" class="start_date edit_time" readonly="readonly" value="' + js_date[i].startDate + '"></span><span class="inp"><input type="text" class="end_date edit_time" readonly="readonly" value = "' + js_date[i].endDate + '"></span></p><button class="btn_del">删除</button><input class="json_date" type="hidden" value = "' + js_date[i].dehours + '"></div>';
        }
        $(".setting .time_box").append(html);
        $(".time_box .time_item .btn_del").bind("click", function (e) {
            $(this).parents(".time_item").remove();
        });
        $(".setting .tr_data").unbind("mousedown").bind("mousedown", function () {
            var date = $("#dateRangePicker").val().split('to');
            var s_date = date[0];
            var e_date = date[1];
            var obj_tr = $(this);
            var th_date_s = $(this).find(".start_date").val();
            var th_date_e = $(this).find(".end_date").val();
            obj_tr.daterangepicker1({
                showDropdowns: true,
                linkedCalendars: true,
                autoUpdateInput: true,
                "startDate": th_date_s,
                "endDate": th_date_e,
                "minDate": s_date,
                "maxDate": e_date,
                locale: {
                    direction: $('#rtl').is(':checked') ? 'rtl' : 'ltr',
                    format: 'YYYY-MM-DD',
                    separator: ' to ',
                    applyLabel: '确定',
                    cancelLabel: '取消',
                    fromLabel: '起始时间',
                    toLabel: '结束时间',
                    daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                    monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                        '七月', '八月', '九月', '十月', '十一月', '十二月'],
                    firstDay: 1
                }
            });
            var obj = $(".daterangepicker1").eq($(".daterangepicker1").length - 1);
            $(".daterangepicker1").eq($(".daterangepicker1").length - 1).find(".quantum_list p").removeClass("pitchon");
            var chose_time = obj_tr.parent().find(".json_date").val();
            if (chose_time != -1 && chose_time != "") {
                var chose_arr = chose_time.split(",");
                for (var n = 0; n < chose_arr.length; n++) {
                    $(".daterangepicker1").eq($(".daterangepicker1").length - 1).find(".quantum_list p").eq(chose_arr[n]).addClass("pitchon");
                }
            }
            index_item = $(this).parent(".time_item").attr("index");
            if (!obj.hasClass("edit")) {
                $(".daterangepicker1").eq($(".daterangepicker1").length - 1).addClass("edit");
                $(".daterangepicker1").eq($(".daterangepicker1").length - 1).attr("index", index_item);
            }
        });
    }
}